<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\Products;

use App\Models\Product\ProductSupplierM;
use Illuminate\Support\Facades\DB;
class MaterialSupplierAjax{

    /**
     * 更新材料说明书的功能性字段
     * @param $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function updateFunction($id){
        $psm = ProductSupplierM::query()->findOrFail($id);
        return view('products.material-supplier.modal-function', compact('id','psm'));
    }

    /**
     * 更新材料说明书的功能性字段
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function doUpdateFunction($param): \Illuminate\Http\JsonResponse
    {
        try{
            DB::beginTransaction();
            $model = ProductSupplierM::query()->findOrFail($param['id']);
            if(!$model || $model->audit_status == 2){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '状态有误！']);
            }
            $model->function = $param['function'];
            $flag = $model->save();
            if(!$flag){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }else{
                DB::commit();
                return response()->json(['status' =>true, 'message' => '操作成功！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 更新材料说明书的成分字段
     * @param $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function updateComposition($id){
        $psm = ProductSupplierM::query()->findOrFail($id);
        return view('products.material-supplier.modal-composition', compact('id','psm'));
    }

    /**
     * 更新材料说明书的成分字段
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function doUpdateComposition($param): \Illuminate\Http\JsonResponse
    {
        try{
            DB::beginTransaction();
            $model = ProductSupplierM::query()->findOrFail($param['id']);
            if(!$model || $model->audit_status == 2){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '状态有误！']);
            }
            $model->composition = $param['composition'];
            $flag = $model->save();
            if(!$flag){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }else{
                DB::commit();
                return response()->json(['status' =>true, 'message' => '操作成功！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }
}
